package com.qf.controller2;

import com.qf.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.stream.Stream;

@Controller
@RequestMapping("/user")
public class UserController {

    /*
       method = RequestMethod.GET 这个资源只能通过get请求访问
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public String findAll(){
        System.out.println("findAll...");

        return "forward:/html/user.html";
    }

    @RequestMapping("/findById")
    public String findById(){
        System.out.println("findById...");

        return "forward:/html/user.html";
    }

    @RequestMapping(value = "/findByName")
    //http://localhost/user/findByName?name=张三
    //http://localhost/user/findByName?username=张三
    // @RequestParam 当参数名称和接收参数的变量名不一致，需要通过这个注解，指定参数的名称
    public String findByName(@RequestParam("username") String name,@RequestParam("age") Integer age,@RequestParam("address") String address){
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("address:" + address);

        return "forward:/html/user.html";
    }

    @RequestMapping("/insert")
    //http://localhost/user/insert?username=张三&age=18&address=杭州
    // 注意：前端发送的参数名称，必须和User实体中的属性名称一一对应
    public String insertUser(User user,@RequestParam("username") String name){
        System.out.println("user:" + user);
        System.out.println("name:" + name);
        return "forward:/html/user.html";
    }

    /*
        springmvc内部，针对时间的转换格式要求是： yyyy/MM/dd-->Date
        传递的值格式是： yyyy-MM-dd  需要使用@DateTimeFormat进行格式限定
     */
    @RequestMapping("/birUser")
    public String insertUserWithBir(User user, @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
        System.out.println("user:" + user);
        System.out.println("bir:" + birthday);

        return "forward:/html/user.html";
    }

    /*
        restful风格的路径：
            更具请求方式+请求路径，区分不同的请求行为
                GET http://localhost:80/user/1   -- 获取id为1的用户信息
                DELETE  http://localhost:80/user/1 -- 删除id为1的用户信息
                PUT http://localhost:80/user/1  -- 修改id为1的用户信息
     */
    //访问资源路径： http://localhost/user/del/2
    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") Integer id){
        System.out.println("id:" + id);
        return "forward:/html/user.html";
    }

    // http://localhost/user/page/1/5
    @RequestMapping("/page/{pageNum}/{pageSize}")
    public String page(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        System.out.println("pageNum:" + pageNum);
        System.out.println("pageSize:" + pageSize);
        return "forward:/html/user.html";
    }

    /*
    请求头：
        user-agent ApiPOST Runtime +https://www.apipost.cn
        connection keep-alive
        token abcdefghijklmn
        Cookie user=zhangsan

     @RequestHeader("token")  获取名称为token的头信息
     @CookieValue("user") 获取cookie中名称为user的cookie值
     */
    @RequestMapping("/hc")
    public String headAndCookie(User user,
                                @RequestHeader("token") String token,
                                @CookieValue("user") String userCookie,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
        System.out.println("user:" + user);
        System.out.println("token:" + token);
        System.out.println("userCookie:" + userCookie);

        System.out.println("bir: " + birthday);

        return "forward:/html/user.html";
    }


    @RequestMapping("/json")
    /*
        @RequestBody
            ① 表示前台传递过来的参数是一个请求体参数 -- 请求方式应该是post
            ② 接收到的是请求体中的Json格式数据，在springmvc底层就会使用Jackson工具，将json字符串格式转成我们需要的java对象
     */
    public String json(@RequestBody User user){
        System.out.println("user:" + user);

        return "forward:/html/user.html";
    }


    @RequestMapping("/reqAndResp")
    public void reqAndResp(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");

        Cookie[] cookies = req.getCookies();
        Stream.of(cookies).forEach((cookie)->{
            String cookieName = cookie.getName();
            String cookieValue = cookie.getValue();

            System.out.println(cookieName + "=" + cookieValue);
        });

        resp.getWriter().write(name);
    }

}
